package Day13;

import java.util.HashMap;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) { // 注意 while 处理多个 case
            int T = in.nextInt();
            int[] ret = new int[T];
            for (int i = 0; i < T; i++) {
                int n = in.nextInt();
                int k = in.nextInt();
                String competition = in.next();
                char[] ch = competition.toCharArray();
                HashMap<Character,Integer> hash = new HashMap<>();
                for (int j = 0; j < n; j++) {
                    hash.put(ch[j], hash.getOrDefault(ch[j],0) + 1);
                    if(ch[j] == 'W'){
                        if(hash.getOrDefault('W',0) < 3){
                            ret[i]++;
                        }else {
                            ret[i] += k;
                        }
                    }else {
                        ret[i]--;
                        hash.put('W',0);
                    }
                }
            }
            for(int i = 0; i < T;i++){
                System.out.println(ret[i]);
            }
        }
    }
}
